package com.example.bakend.service.Impl;

import com.example.bakend.mapper.ReaderMapper;
import com.example.bakend.pojo.Book;
import com.example.bakend.pojo.PageInfo;
import com.example.bakend.pojo.Reader;
import com.example.bakend.service.ReaderService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ReaderServiceImpl implements ReaderService {
    @Autowired
    ReaderMapper readerMapper;


    @Override
    public PageInfo selectReaders(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo();
        List<Reader> readers = readerMapper.selectReaders();
        Page<Reader> page = (Page<Reader>) readers;
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setRows(page.getResult());
        return pageInfo;
    }

    @Override
    public List<Reader> selectReaders() {
        return readerMapper.selectReaders();
    }

    @Override
    public Reader selectReader(Integer id) {
        return readerMapper.selectReader(id);
    }

    @Override
    public Integer insertReader(Reader reader) {
        reader.setCreateTime(LocalDateTime.now());
        reader.setUpdateTime(LocalDateTime.now());
        return readerMapper.insertReader(reader);
    }

    @Override
    public Integer updateReader(Reader reader) {
        reader.setUpdateTime(LocalDateTime.now());
        return readerMapper.updateReader(reader);
    }

    @Override
    public Integer deleteReaders(List<Integer> ids) {
        return readerMapper.deleteReaders(ids);
    }

    @Override
    public PageInfo getReaders(String name, String gender, String contact, LocalDateTime updateTimeBegin, LocalDateTime updateTimeEnd, Integer pageNum,
                               Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo pageInfo = new PageInfo();
        List<Reader> Readers = readerMapper.getReaders(name, gender, contact, updateTimeBegin, updateTimeEnd);
        Page<Reader> page = (Page<Reader>) Readers;
        pageInfo.setTotal((int) page.getTotal());
        pageInfo.setRows(page.getResult());
        return pageInfo;
    }


}
